local Enemy = require("Controller/Enemy");

local Boss = class("Boss", Enemy);

local speed = 1;
local bulletSpeed = 5;
local attackDuration = 4;

local AIType = {};
AIType.Enter = 0;
AIType.Move = 1;
AIType.Attack = 2;

function Boss:ctor(go)
	self.gameObject = go;
	self.transform = go.transform;
	self.firePoint = self.transform:Find("firepoint");
	self.hp = 5000;
	self.lastFireTime = 0;
	self.lastAttackTime = 0;
	
	self.aiType = AIType.Enter;
	
	local luaTrigger = self.gameObject:AddComponent(typeof(LuaTrigger2D));
	luaTrigger:SetExitFunc(self.OnTriggerExit, self);
	luaTrigger:SetEnterFunc(self.OnTriggerEnter, self);
	
	UpdateBeat:Add(self.Update, self);
end

function Boss:Update()
	if(self.aiType == AIType.Enter) then
		self:Enter();
	elseif(self.aiType == AIType.Move) then
		self:Move();
	elseif(self.aiType == AIType.Attack) then
		self:Attack();
	end
end

function Boss:Enter()
	self.transform:Translate(Vector3.down * speed * Time.deltaTime);
	if(self.transform.position.y <= 2) then
		self.aiType = AIType.Attack;
		self.lastAttackTime = Time.time;
	end
end

function Boss:Move()
	local vect = PlayerManager.GetPos() - self.transform.position;
	if(math.abs(vect.x) < 0.2) then
		self.aiType = AIType.Attack;
		self.lastAttackTime = Time.time;
	else
		if(vect.x<=0)then
			self.transform:Translate(Vector3.left * speed * Time.deltaTime);
		else
			self.transform:Translate(Vector3.right * speed * Time.deltaTime);
		end
	end
end

function Boss:Attack()
	self:Fire();
	print(Time.time - self.lastAttackTime);
	if(Time.time - self.lastAttackTime > attackDuration) then
		self.aiType = AIType.Move;
	end
end

function Boss:Fire()
	if(Time.time - self.lastFireTime > 0.5) then
		self.lastFireTime = Time.time;
		
		--创建子弹，并发射
		local go = poolMgr:Get("EnemyBullet");
		go.transform.position = self.firePoint.position;
		go.transform.rotation = self.firePoint.rotation;
		
		local body = go:GetComponent('Rigidbody2D');
		body.velocity = self.firePoint.up * bulletSpeed;
	end
end

return Boss; 